package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/item")
@Slf4j
public class ItemController {

    @Autowired
    private IItemService itemService;


    @GetMapping("/list")
    public PageDTO pageResult(Long page,Long size) {
        log.info("分页参数：{},{}",page,size);
        PageDTO pageDTO=itemService.pageQuery(page,size);
        return pageDTO;
    }



    @GetMapping("/{id}")
    public Item selectById(@PathVariable Long id) {
        log.info("根据id查询商品：{}",id);
        Item item=itemService.selectById(id);
        return item;
    }

    @PostMapping
    public void save(@RequestBody Item item) {
        log.info("新增商品：{}",item);
        itemService.saveproduct(item);
    }

    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable Long id,@PathVariable Integer status) {
        log.info("修改商品状态：{},{}",id,status);
        itemService.updateStatus(id,status);
    }

    @PutMapping
    public void update(@RequestBody Item item) {
        log.info("修改商品：{}",item);
        itemService.updateAll(item);
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable Long id) {
        log.info("根据id删除：{}",id);
        itemService.deleteById(id);
    }

    //扣减库存
    @PutMapping("/stock/{itemId}/{num}")
    public void  stock(@RequestParam("itemId") Long itemId,@RequestParam("num")Integer num){
        Item byId = itemService.selectById(itemId);
        Long id = byId.getId();
        Integer status = byId.getStatus();
        Item item = new Item();
        item.setId(id);
        item.setStock(status);
        itemService.updateById(item);
    }
}
